__author__ = 'Daan'

import xmlrpclib

def printVerbose( istr, arg, args, kwargs, verbose ):
    if verbose:
        arg = arg + (args, kwargs)
        print istr.format( *arg )


class ServerProxy_stub( object ):
    def __init__( self, verbose = None ):
        self.verbose = verbose or False
        self.original_proxy = None
        self.original_binary = None

        self.registerMachineList = []
        self.storeFileList = []

    def stub( self, registerMachineReturn = None, storeFileReturn = None, machineListReturn = None ):
        registerMachineReturn = registerMachineReturn or True
        storeFileReturn = storeFileReturn or True
        machineListReturn = machineListReturn or []

        this = self

        #noinspection PyUnusedLocal
        class serverproxystub( object ):
            def __init__( self, uri, transport = None, encoding = None, verbose = 0,
                         allow_none = 0, use_datetime = 0 ):
                pass

            def storeFile( self, name, info, xmlrpcBinary, *args, **kwargs ):
                printVerbose( "storeFile name={0} info={1} xmlrpcBinary={2} args=({3}) kwargs=({4})", (name, info, xmlrpcBinary.__class__), args, kwargs, this.verbose )

                return storeFileReturn

            def retrieveMachines( self, *args, **kwargs ):
                printVerbose( "retrieveMachines args=({0}) kwargs=({1})", tuple(), args, kwargs, this.verbose )

                return machineListReturn

            def registerMachine( self, machine, *args, **kwargs ):
                printVerbose( "registerMachine machine=%s args=({0}) kwargs=({1})" % machine, tuple(), args, kwargs, this.verbose )

                this.registerMachineList.append( machine )

                return registerMachineReturn

            def shutdown( self, *args, **kwargs ):
                printVerbose( "shutdown args=({0}) kwargs=({1})", tuple(), args, kwargs, this.verbose )

        xmlrpclib.ServerProxy = serverproxystub

    def stub_error( self, exc_type, exc_msg ):

        this = self

        #noinspection PyUnusedLocal
        class serverproxystub( object ):
            def __init__( self, uri, transport = None, encoding = None, verbose = 0,
                         allow_none = 0, use_datetime = 0 ):
                pass

            def storeFile( self, *args, **kwargs ):
                printVerbose( "storeFile args=({0}) kwargs=({1})", tuple(), args, kwargs, this.verbose )

                raise exc_type

            def retrieveMachines( self, *args, **kwargs ):
                printVerbose( "retrieveMachines args=({0}) kwargs=({1})", tuple(), args, kwargs, this.verbose )

                raise exc_type

            def registerMachine( self, machine, *args, **kwargs ):
                printVerbose( "registerMachine machine=%s args=({0}) kwargs=({1})" % machine, tuple(), args, kwargs, this.verbose )

                this.registerMachineList.append( machine )

                raise exc_type

            def shutdown( self, *args, **kwargs ):
                printVerbose( "shutdown args=({0}) kwargs=({1})", tuple(), args, kwargs, this.verbose )

                raise exc_type

        xmlrpclib.ServerProxy = serverproxystub


    def __enter__( self ):
        self.original_proxy = xmlrpclib.ServerProxy
        self.original_binary = xmlrpclib.Binary
        return self

    def __exit__( self, exc_type, exc_val, exc_tb ):
        xmlrpclib.ServerProxy = self.original_proxy
        xmlrpclib.Binary = self.original_binary
